<?php
class Barang extends CI_Controller{

    private $limit = 10;
	function  __construct() {
    parent::__construct();
    $this->load->model('Barang_model');
	$this->load->helper(array('form', 'url'));
	$this->load->library(array('form_validation', 'pagination'));
    }

    function index(){

        $this->load->library('pagination'); //load library pagination
		$uri_segment = 3;
		$offset = $this->uri->segment($uri_segment);
		$config['base_url'] = site_url('barang/index/');
		$config['total_rows'] = $this->Barang_model->countData();
		$config['per_page'] = $this->limit;
		$config['num_links'] = 2;
		$config['full_tag_open'] = '<div id="pagination">';
		$config['full_tag_close'] = '</div>';

		$this->pagination->initialize($config);
		$data['pagination'] = $this->pagination->create_links();
		$data['offset']=$offset;
		$data['jumlah_data']=$this->Barang_model->countData();
		if($_POST){
		$data['barang']	= $this->Barang_model->get_list_data_filter($this->limit,$offset, $_POST);	
		}else{
		$data['barang']	= $this->Barang_model->get_list_data($this->limit,$offset);
		}
	    $this->load->view('admin_views/barang/barang', $data);
    }

     

    function detail($id){
		$profile = $this->Barang_model->selectDetail($id);
        	foreach($profile as $m)
			{
				$data['strnama']		= $m->strnama;
				$data['intharga_jawa']		= $m->intharga_jawa;
				$data['intpv_jawa']		= $m->intpv_jawa;
				$data['intharga_luarjawa']	= $m->intharga_luarjawa;
				$data['intpv_luarjawa']  	= $m->intpv_luarjawa;
				$data['strnama_jbarang']       	= $m->strnama_jbarang;
				$data['strnama_jsatuan']       	= $m->strnama_jsatuan;
                $data['qty']       	= $m->intqty;
            }
                         
		$this->load->view('admin_views/barang/detail', $data);
	}
        //end test

	function add()
	{
		$this->form_validation->set_rules('strnama', 'Nama Barang', 'trim|required|xss_clean');
		//$this->form_validation->set_rules('intharga_jawa', 'Harga Barang P.Jawa', 'trim|required|xss_clean');
		//$this->form_validation->set_rules('intharga_luarjawa', 'Harga Barang Luar Jawa', 'trim|required|xss_clean');
		//$this->form_validation->set_rules('intpv_jawa', 'PV Barang P.Jawa', 'trim|required|xss_clean');
		//$this->form_validation->set_rules('intpv_luarjawa', 'PV Barang Luar Jawa', 'trim|required|xss_clean');
		$this->form_validation->set_rules('intid_jsatuan', 'Jenis Satuan', 'trim|required|xss_clean');
		$this->form_validation->set_rules('intid_jbarang', 'Jenis Barang', 'trim|required|xss_clean');
        //$this->form_validation->set_rules('intqty', 'Quantity Barang', 'trim|required|xss_clean');

		$this->form_validation->set_message('required', ' %s tidak boleh kosong !!');

		if ($this->form_validation->run() == FALSE)
		{
			$jbarang = $this->Barang_model->selectJbarang();
			foreach ($jbarang as $g)
			{
				$data['idjb'][]	 	= $g->intid_jbarang;
				$data['namajb'][] 	= $g->strnama_jbarang;
			}
			$jsatuan = $this->Barang_model->selectJsatuan();
			foreach ($jsatuan as $g)
			{
				$data['idjs'][]	 	= $g->intid_jsatuan;
				$data['namajs'][] 	= $g->strnama_jsatuan;
			}
			$this->load->view('admin_views/barang/add', $data);

		}else {
			$this->Barang_model->insert($_POST);
			
			redirect('barang/index');
		}
	}
		function edit($intid_barang)
		{
			if($_POST==NULL)
			{
			$jbarang = $this->Barang_model->selectJbarang();
			foreach ($jbarang as $g)
			{
				$data['idjb'][]	 	= $g->intid_jbarang;
				$data['namajb'][] 	= $g->strnama_jbarang;
			}
			$jsatuan = $this->Barang_model->selectJsatuan();
			foreach ($jsatuan as $g)
			{
				$data['idjs'][]	 	= $g->intid_jsatuan;
				$data['namajs'][] 	= $g->strnama_jsatuan;
			}
				$databarang = $this->Barang_model->select($intid_barang);
				foreach ($databarang as $g)
				{
					$data['intid_barang']	 	= $g->intid_barang;
					$data['intid_jbarang'] 		= $g->intid_jbarang;
					$data['intharga_jawa']	 	= $g->intharga_jawa;
					$data['intpv_jawa']	 	= $g->intpv_jawa;
					$data['intharga_luarjawa']	= $g->intharga_luarjawa;
					$data['intpv_luarjawa']         = $g->intpv_luarjawa;
					$data['intum_jawa']	 	= $g->intum_jawa;
					$data['intum_luarjawa']	 	= $g->intum_luarjawa;
					$data['intcicilan_jawa']	 	= $g->intcicilan_jawa;
					$data['intcicilan_luarjawa']	 	= $g->intcicilan_luarjawa;
					
					$data['strnama'] 		= $g->strnama;
					$data['intid_jsatuan']  	= $g->intid_jsatuan;
                    //$data['intqty']  	= $g->intqty;
					
				}
				$this->load->view('admin_views/barang/edit', $data);
			}else {

				$this->Barang_model->update($intid_barang);
				redirect('barang/index');
			}

		}

	function delete($intid_barang)
	{
		$this->Barang_model->delete($intid_barang);
		redirect('barang/index');
	}

    

}
?>
